package com.rc.saas.tenant.controller.statistics;

import com.alibaba.excel.EasyExcel;
import com.rc.saas.tenant.common.constant.CommonConstant;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.model.elec.ElecStationPrice;
import com.rc.saas.tenant.model.elec.ElecStationPriceExample;
import com.rc.saas.tenant.model.tenant.TenantUser;
import com.rc.saas.tenant.service.elec.ElecStationPriceService;
import com.rc.saas.tenant.service.order.OrderDetailService;
import com.rc.saas.tenant.service.order.OrderInfoService;
import com.rc.saas.tenant.shiro.ShiroUtils;
import com.rc.saas.tenant.util.ExportUtils;
import com.rc.saas.tenant.vo.order.OrderDetailPrintVo;
import com.rc.saas.tenant.vo.statistics.OperatorPowerTimePeriodVo;
import com.rc.saas.tenant.vo.statistics.StatisticsQueryVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author frog
 * @date 2021/7/30
 */
@Controller
@RequestMapping("/operatorPowerTimePeriod")
public class OperatorPowerTimePeriodController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());


    @Autowired
    private OrderInfoService orderInfoService;

    @Autowired
    private ElecStationPriceService elecStationPriceService;

    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 跳转分销商峰平谷电量统计
     *
     * @return
     */
    @RequestMapping(value = "/index/tenantOperatorCode", method = RequestMethod.GET)
    public String operatorPowerTimePeriod(Model model) {
        TenantUser sessionUser = ShiroUtils.getSessionUser();
        model.addAttribute("userType", CommonConstant.TENANT_OPERATOR_CODE);
        model.addAttribute("code", sessionUser.getTenantOperatorCode());
        return "/templates/statistics/operatorPowerTimePeriod";
    }


    /**
     * 返回充电量统计数据
     *
     * @return
     */
    @RequestMapping(value = "/listOperatorPowerTimePeriod", method = RequestMethod.GET)
    @ResponseBody
    public List<OperatorPowerTimePeriodVo> listOperatorPowerTimePeriod(StatisticsQueryVo statisticsQueryVo) {
        List<ElecStationPrice> stationPriceList = listElecStationPrice(statisticsQueryVo);
        if (stationPriceList == null) return null;
        List<OperatorPowerTimePeriodVo> list = orderInfoService.listOperatorPowerTimePeriod(stationPriceList, statisticsQueryVo);
        return list;
    }

    /**
     * 导出 电量统计 分段订单
     */
    @ResponseBody
    @RequestMapping(value = "/exportOperatorPowerTimePeriod", method = RequestMethod.GET)
    public TResponse exportOperatorPowerTimePeriod(StatisticsQueryVo statisticsQueryVo, HttpServletResponse response) {
        try {
            response.setHeader("Set-Cookie", "fileDownload=true; path=/");
            ExportUtils.setDefaultPaging(statisticsQueryVo);
            List<ElecStationPrice> stationPriceList = listElecStationPrice(statisticsQueryVo);
            if (stationPriceList == null) return null;

            List<OrderDetailPrintVo> list = orderDetailService.exportOperatorPowerTimePeriod(stationPriceList, statisticsQueryVo);

            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("订单列表", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), OrderDetailPrintVo.class).sheet("订单列表").doWrite(list);
            return TResponse.SUCCESS();

        } catch (Exception e) {
            logger.error("导出订单失败", e.getMessage());
            return TResponse.FAIL(e.getMessage());
        }
    }


    private List<ElecStationPrice> listElecStationPrice(StatisticsQueryVo statisticsQueryVo) {
        ElecStationPriceExample example = new ElecStationPriceExample();
        example.createCriteria().andElecStationIdEqualTo(statisticsQueryVo.getElecStationId());
        List<ElecStationPrice> stationPriceList = elecStationPriceService.selectByExample(example);
        if (CollectionUtils.isEmpty(stationPriceList)) {
            return null;
        }
        return stationPriceList;
    }


}
